/**
 * Created by ljg on 2017/2/22.
 */
import uiSearchForm from './searchform';
import uiTable from './uitable';
import tablePage from './tablepage';
import searchInput from './searchinput';
import uiTableTool from './uitabletool';
import searchSelect from './searchselect';
import searchRegion from './searchregion';
import searchSelectRemote from './searchselectremote';
import searchDate from './searchdate';

const uiContainer=function () {
    angular.module('ui.table',['ui.help']);
    uiSearchForm();
    uiTable();
    tablePage();
    searchInput();
    uiTableTool();
    searchSelect();
    searchRegion();
    searchSelectRemote();
    searchDate();

    angular.module('ui.table')
        .directive('uiContainer', function (eventHelper) {
            return {
                restrict: 'E',
                replace: true,
                transclude: true,
                controller:function ($scope,$attrs) {
                    //获取表格操作按钮
                    this._getBtns=function () {
                        return $scope.$parent._btns||[];
                    };
                    //获取列展示数据
                    this._getColumns=function () {
                        return $scope.$parent._columns||[];
                    };

                    //获取操作列数据
                    this._getOperateColumns=function () {
                        return $scope.$parent._operateColumns||[];
                    };
                    this._getFormParams=function () {
                        const formParam=$("#table_form_"+key).serializeArray()||[];
                        const param={};
                        formParam.map(f=>{
                            param[f.name]=f.value;
                        });
                        return param;
                    }
                    const key=$attrs.key||'uicontainer';
                    //获取key
                    this.key=key;
                    const _this=this;
                    //查询table结果dataList
                    this._queryData=function (param) {
                        const formParam=$("#table_form_"+key).serializeArray()||[];

                        const pageNo=param.pageNo==undefined||param.pageNo<=0?0:param.pageNo;
                        const pageSize=param.pageSize==undefined?10:param.pageSize;

                        const pageParam={pageNo,pageSize};
                        formParam.map(f=>{
                            pageParam[f.name]=f.value;
                        });
                        $.ajax({
                            url:$attrs.url,
                            type:$attrs.method||'post',
                            data:pageParam

                        }).done(function (result) {
                            const data=result.data||{};
                            const totalCount=data.totalRecord;
                            pageParam['totalCount']=totalCount;
                            _this._dataList=data.dataList;
                            eventHelper.downNotice($scope,'ui.table.getData',data.dataList,pageParam);
                        }).fail(function (r) {
                            console.info('fail')
                            console.info(r)
                        });

                    };
                    $scope.$parent[key]=this;


                    this._selectedItems=function (selectedItems) {
                        eventHelper.downNotice($scope,'ui.table.selectedItems',selectedItems);
                    }
                    if($attrs.auto){
                        _this._queryData({});
                    }

                },
                scope:{},
                template: function(el,attr){
                    return  '<div class="panel panel-default">'+
                                '<div class="panel-body" ng-transclude>'+
                                '</div>'+
                            '</div>';
                },
                link : function(scope, element, attrs) {
                }
            };
        });
}
export default uiContainer;
